<template>
  <div class="message-detail-page">
    <div class="navigator">
      <div class="tabs">
        <div
          class="tab-item"
          v-for="(tab, index) in tabs"
          :key="tab.key"
          :class="{ active: activeTab === index }"
          @click="changeActiveTab(index)"
        >
          {{ tab.title }}
        </div>
      </div>
      <div class="handler">
        <div
          class="btn-edit"
          v-if="editBtnVisible"
          @click="drawerVisible = true"
        >
          编辑通知
        </div>
        <img
          src="@/assets/images/message/icon-back.svg?url"
          class="btn-back"
          @click="goBack"
        />
      </div>
    </div>
    <component :is="tabs[activeTab].component" :msgInfo="msgInfo"></component>
  </div>

  <MessageDrawer
    v-if="drawerVisible"
    v-model:visible="drawerVisible"
    :msgType="msgInfo.type"
    :editInfo="editInfo"
    @updateMessageDetail="getMsgDetail"
  />
</template>

<script setup>
import MessageInfo from '@/views/message-center/components/MessageInfo.vue'
import MessageRead from '@/views/message-center/components/MessageRead.vue'
import MessageDrawer from '@/views/message-center/components/MessageDrawer.vue'
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { reqGetMsgDetail } from '@/apis/message'
import { MsgStatusEnum } from '../constant'

const tabs = [
  {
    title: '通知详情',
    key: 'info',
    component: MessageInfo
  },
  {
    title: '查阅情况',
    key: 'status',
    component: MessageRead
  }
]
const activeTab = ref(0)
const changeActiveTab = newVal => {
  activeTab.value = newVal
}

const editBtnVisible = computed(
  () =>
    route.query.release === MsgStatusEnum.UNPUBLISHED.toString() &&
    activeTab.value === 0
)

const route = useRoute()
const router = useRouter()

const goBack = () => {
  router.back()
}

const msgInfo = ref({})
const getMsgDetail = async () => {
  const params = {
    message_id: route.query.msgId,
    release: route.query.release
  }
  const res = await reqGetMsgDetail(params)
  msgInfo.value = res
}

getMsgDetail()

const editInfo = computed(() => ({
  ...msgInfo.value.request_param,
  message_id: msgInfo.value.message_id
}))
const drawerVisible = ref(false)
</script>

<style lang="less" scoped>
.message-detail-page {
  height: calc(100vh - 30px);

  .navigator {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 34px;
    font-size: 16px;

    .tabs {
      display: flex;
      .tab-item {
        position: relative;
        margin-right: 40px;
        font-size: 22px;
        cursor: pointer;
        &.active {
          font-weight: bold;
          color: @color-brand-regular;
          &::after {
            position: absolute;
            bottom: -8px;
            left: 50%;
            transform: translateX(-50%);
            content: '';
            width: 28px;
            height: 6px;
            border-radius: 6px;
            background-color: #bed7ff;
          }
        }
      }
    }

    .handler {
      display: flex;
      align-items: center;
      .btn-edit {
        padding: 5px 16px;
        margin-right: 26px;
        color: #fff;
        border-radius: 3px;
        background-color: #428fff;
        cursor: pointer;
      }
      .btn-back {
        width: 52px;
        cursor: pointer;
      }
    }
  }
}
</style>
